package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryDocumentListByIdsReq;
import com.sg.dto.base.component.req.BatchQueryDocumentReq;
import com.sg.dto.base.component.req.QueryDocumentDetailReq;
import com.sg.dto.base.component.req.QueryDocumentListReq;
import com.sg.entity.OmsUnstructuredData;
import com.sg.service.base.component.MOmsUnstructuredDataService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsUnstructuredDataMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsUnstructuredDataServiceImpl
        extends ServiceImpl<OmsUnstructuredDataMapper, OmsUnstructuredData>
        implements MOmsUnstructuredDataService {

    @Autowired
    OmsUnstructuredDataMapper omsUnstructuredDataMapper;

    /**
     * 2-7-01批量查询文档列表[2669]
     * gen by moon at 1/23/2024, 3:19:45 PM
     */
    @Trace(operationName = "2-7-01批量查询文档列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredData> batchQueryDocument(BatchQueryDocumentReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredData> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getDocumentList() == null || reqDto.getDocumentList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("unstructured_data_id", reqDto.getDocumentList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredData.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsUnstructuredData>) super.list(queryWrapper)).stream().map(item -> {

                item.setFileRelativePathAccess(item.getFileRelativePathAccess());
//                 item.setFileRelativeAllPathAccess(CommonFunctionHelper.getFilePath(item.getFileRelativePathAccess()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-01新增文档[2671]
     * gen by moon at 3/27/2024, 12:58:42 AM
     */
    @Trace(operationName = "2-7-01新增文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addDocument(OmsUnstructuredData omsUnstructuredData) {
// TODO: 2024/1/23

//     if(!StringUtils.isEmpty(omsUnstructuredData.getFileRelativePathAccess())){
//         omsUnstructuredData.setFileRelativePathAccess(CommonFunctionHelper.replaceFilePath(omsUnstructuredData.getFileRelativePathAccess()));
//     }
        omsUnstructuredData.setUnstructuredDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsUnstructuredData.setUnstructuredDataNumber(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsUnstructuredData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredData.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredData.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsUnstructuredData.setIsValid("FALSE");
        omsUnstructuredData.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsUnstructuredData)) {
            return omsUnstructuredData.getUnstructuredDataId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-7-01修改文档[2869]
     * gen by moon at 10/10/2022, 9:53:11 PM
     */
    @Trace(operationName = "2-7-01修改文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateDocument(OmsUnstructuredData omsUnstructuredData) {

        omsUnstructuredData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsUnstructuredData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsUnstructuredData);
    }

    /**
     * 2-7-01批量删文档[2908]
     * gen by moon at 10/10/2022, 9:53:12 PM
     */
    @Trace(operationName = "2-7-01批量删文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteDocument(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-7-01查询文档详情[3169]
     * gen by moon at 3/27/2024, 12:58:42 AM
     */
    @Trace(operationName = "2-7-01查询文档详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsUnstructuredData queryDocumentDetail(QueryDocumentDetailReq reqDto) {

        try {

            QueryWrapper<OmsUnstructuredData> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsUnstructuredData> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsUnstructuredData retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-01删除文档[3241]
     * gen by moon at 10/10/2022, 9:53:13 PM
     */
    @Trace(operationName = "2-7-01删除文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDocument(String unstructuredDataId) {

        return super.removeById(unstructuredDataId);
    }

    /**
     * 2-7-01批量修改文档[3338]
     * gen by moon at 10/10/2022, 9:53:13 PM
     */
    @Trace(operationName = "2-7-01批量修改文档")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateDocument(List<OmsUnstructuredData> omsUnstructuredData) {

        if (omsUnstructuredData == null || omsUnstructuredData.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsUnstructuredData);

    }

    /**
     * 2-7-01查询文档列表[8821]
     * gen by moon at 7/25/2024, 9:38:33 PM
     */
    @Trace(operationName = "2-7-01查询文档列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredData> queryDocumentList(QueryDocumentListReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredData> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredData.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsUnstructuredData>) super.list(queryWrapper)).stream().map(item -> {
                item.setFileRelativePathAccess(CommonFunctionHelper.getFilePath(item.getFileRelativePathAccess()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-7-01批量查询文档列表By文件夹Ids[8800]
     * gen by moon at 3/27/2024, 10:27:55 PM
     */
    @Trace(operationName = "2-7-01批量查询文档列表By文件夹Ids")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsUnstructuredData> batchQueryDocumentListByIds(BatchQueryDocumentListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsUnstructuredData> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getDataFolderList() == null || reqDto.getDataFolderList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("unstructured_data_folder_id", reqDto.getDataFolderList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsUnstructuredData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsUnstructuredData.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsUnstructuredData>) super.list(queryWrapper)).stream().map(item -> {
                item.setFileRelativePathAccess(CommonFunctionHelper.getFilePath(item.getFileRelativePathAccess()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

}

